package com.zzj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzj.bean.TabFlower;
import com.zzj.bean.TabInventory;
import com.zzj.bean.TabSupplier;
import com.zzj.service.TabFlowerService;
import com.zzj.service.TabInventoryService;
import com.zzj.service.TabSupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * InnoDB free: 8192 kB 前端控制器
 * </p>
 *
 * @author 花
 * @since 2022-03-04
 */
@CrossOrigin
@Api
@RestController
@RequestMapping("/inventory")
public class TabInventoryController {

    @Resource
    private TabInventoryService tabInventoryService;
    @Resource
    private TabFlowerService tabFlowerService;
    @Resource
    private TabSupplierService tabSupplierService;

    @ApiOperation("修改花数量或给仓库添加新的花")
    @PostMapping("update")
    public boolean updateCount(@RequestParam(defaultValue = "0") String istatus,
                               @RequestBody TabSupplier tabSupplier){
//
        tabSupplierService.updateSustatus(tabSupplier.getSuid(),tabSupplier.getSustatus());
//        给库存添加
        TabInventory iEntiy=new TabInventory();
        iEntiy.setFname(tabSupplier.getFname());
        iEntiy.setIcount(tabSupplier.getSucount());
        iEntiy.setIstatus(istatus);

        return tabInventoryService.updateCount(iEntiy);
    }

    @ApiOperation("查找库存中所有的花信息")
    @GetMapping("findAllInventory")
    public Page<TabInventory> findAllInventory(@RequestParam(defaultValue = "1") Integer pageNum,
                                               @RequestParam(defaultValue = "5") Integer pageSize){
        Page<TabInventory> page=new Page<>(pageNum,pageSize);
        return tabInventoryService.page(page);

    }

    @ApiOperation("下架商品")
    @PutMapping("soldOut")
    public boolean updateStatus(@RequestBody TabInventory tabInventory){

//      下架商店中的花
        QueryWrapper<TabFlower> wrapper = new QueryWrapper<>();
        wrapper.eq("fname",tabInventory.getFname());
        tabFlowerService.remove(wrapper);

        //      修改状态
        return tabInventoryService.updateStatus(tabInventory);

    }

    @ApiOperation("上架商品")
    @PostMapping("putAway/{price}/{kid}")
    public boolean putAway( @RequestBody TabInventory inven,
                            @PathVariable("price") Double price,@PathVariable("kid") String kid){
//        System.out.println(kid);
//        System.out.println(price);
//        System.out.println(inven);
//        修改状态
        tabInventoryService.updateStatus(inven);
////        上架商品
//        TabFlower tabFlower=new TabFlower();
//        tabFlower.setFname(inven.getFname());
//        tabFlower.setKid(kid);
//        tabFlower.setPrice(price);
//        tabFlowerService.save(tabFlower);
//        return  true;

        System.out.println(inven);
        String fname = inven.getFname();
        System.out.println(fname);
        //        查询该商品的产地
        QueryWrapper<TabSupplier> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("fname",fname);
        List<TabSupplier> list = tabSupplierService.list(queryWrapper);
        System.out.println(list);
//       封装   给商品每一项赋值
        TabFlower tabFlower =new TabFlower();
        tabFlower.setFname(inven.getFname());    //花名
        tabFlower.setFplace(list.get(0).getSuplace());  //花产地
        tabFlower.setPrice(price);    //售价
        tabFlower.setKid(kid);      //花种类id
        return tabFlowerService.save(tabFlower);

    }

    @ApiOperation("模糊查询")
    @PostMapping("fuzzyQuery")
    public Page<TabInventory> fuzzyQuery(@RequestBody Map<String,String> map){
        Page<TabInventory> page = new Page<>();

        QueryWrapper<TabInventory> wrapper = new QueryWrapper<>();
        wrapper.like(map.get("key"),map.get("value"));

        return tabInventoryService.page(page,wrapper);
    }

}

